<?php

/**
 * Klasa walidująca poprawność hasła
 *
 * @category	Core
 * @package		Core_Validate
 * @copyright	Copyright (c) 2008 Kuśmierz Adam
 */

class Core_Validate_Password extends Zend_Validate_Abstract
{
	const NOT_MATCH = 'PasswordConfirmNotMatch';
	
	/**
	 * @var array
	*/
	protected $_messageTemplates = array(
		self::NOT_MATCH => 'Password confirmation does not match'
	);
	
	 /**
	  * @var array
	  */
	protected $_messageVariables = array(
		'passwordConfirm' => '_passwordConfirm'
	);
	
	/**
	 * Password confirmation name
	 *
	 * @var string
	*/
	protected $_passwordConfirm;
	
	/**
	 * Sets validator options
	 *
	 * @param string $passwordConfirm
	 * @return void
	 */
	public function __construct($passwordConfirm = 'password')
	{
		$this->setPasswordConfirm($passwordConfirm);
	}
	
	/**
	 * Returns the passwordConfirm option
	 *
	 * @return string
	 */
	public function getPasswordConfirm()
	{
		return $this->_passwordConfirm;
	}
	
	/**
	 * Sets the passwordConfirm option
	 *
	 * @param  string $passwordConfirm
	 * @return Core_Validate_Password Provides a fluent interface
	 */
	public function setPasswordConfirm($passwordConfirm = 'password')
	{
		$this->_passwordConfirm = $passwordConfirm;
		return $this;
	}
	
	/**
	 * Defined by Zend_Validate_Interface
	 *
	 * Returns true if and only if the password exact match with passwordConfirm value.
	 *
	 * @param  string $value
	 * @return boolean
	 */
	public function isValid($value, $context = null)
	{
		$value = (string) $value;
		$this->_setValue($value);
		if (is_array($context)) {
			if (isset($context[$this->_passwordConfirm])
			&& ($value == $context[$this->_passwordConfirm])) {
				return true;
			}
		} elseif (is_string($context) && ($value == $context)) {
			return true;
		}
		
		$this->_error(self::NOT_MATCH);
		return false;
	}
}